package zzs.generalframework.console.core.authorization.authfunction.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.authorization.authfunction.AuthFunctionBO;
import zzs.generalframework.common.core.authorization.authfunction.AuthFunctionInfo;
import zzs.generalframework.common.core.authorization.authfunction.AuthFunctionPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.authorization.authfunction.dao.IAuthFunctionDao;
import zzs.generalframework.console.core.authorization.authfunction.service.IAuthFunctionService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.authorization.authfunction.service.impl
 * @author zzs
 * @date 2018年1月31日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.authorization.authfunction.service.impl.authFunctionServiceImpl")
public class AuthFunctionServiceImpl implements IAuthFunctionService
{
	private Logger logger = LoggerFactory.getLogger(AuthFunctionServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IAuthFunctionDao authFunctionDao;

	@Override
	public ConsoleReturnBO saveAuthFunction(AuthFunctionBO authFunctionBO)
	{
		logger.debug("====保存权限功能信息开始====");
		logger.debug("====保存权限功能信息.入参:AuthFunctionBO=[{}]====", authFunctionBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthFunctionPO authFunctionPO = new AuthFunctionPO();
		copyAuthFunctionBO2AuthFunctionPO(authFunctionBO, authFunctionPO);
		Boolean insertResult = authFunctionDao.insert(authFunctionPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存权限功能信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存权限功能信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveAuthFunction(List<AuthFunctionBO> authFunctionBOList) throws ConsoleException
	{
		logger.info("====保存权限功能信息列表开始====");
		logger.debug("====保存权限功能信息列表.入参:AuthFunctionBOList=[{}]====", authFunctionBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthFunctionBO itemBO : authFunctionBOList)
		{
			ConsoleReturnBO itemResult = saveAuthFunction(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存权限功能信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存权限功能信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getAuthFunction(AuthFunctionInfo authFunctionInfo)
	{
		logger.info("====获取权限功能信息开始====");
		logger.debug("====获取权限功能信息.入参:AuthFunctionInfo=[{}]====", authFunctionInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AuthFunctionPO> authFunctionPOList = authFunctionDao.select(authFunctionInfo);
		if (authFunctionPOList == null || authFunctionPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (authFunctionPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			AuthFunctionBO authFunctionBO = new AuthFunctionBO();
			copyAuthFunctionPO2AuthFunctionBO(authFunctionPOList.get(0), authFunctionBO);
			returnConsoleReturnBO.setReturnObj(authFunctionBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			AuthFunctionBO authFunctionBO = new AuthFunctionBO();
			copyAuthFunctionPO2AuthFunctionBO(authFunctionPOList.get(0), authFunctionBO);
			returnConsoleReturnBO.setReturnObj(authFunctionBO);
		}
		logger.debug("====获取权限功能信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取权限功能信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listAuthFunction(AuthFunctionInfo authFunctionInfo)
	{
		logger.info("====获取权限功能信息列表开始====");
		logger.debug("====获取权限功能信息列表.入参:AuthFunctionInfo=[{}]====", authFunctionInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AuthFunctionPO> authFunctionPOList = authFunctionDao.select(authFunctionInfo);
		if (authFunctionPOList == null || authFunctionPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<AuthFunctionBO> resultList = new ArrayList<AuthFunctionBO>();
			for (AuthFunctionPO itemPO : authFunctionPOList)
			{
				AuthFunctionBO itemBO = new AuthFunctionBO();
				copyAuthFunctionPO2AuthFunctionBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取权限功能信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取权限功能信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAuthFunction(AuthFunctionBO authFunctionBO)
	{
		logger.info("====更新权限功能信息开始====");
		logger.debug("====更新权限功能信息.入参:AuthFunctionBO=[{}]====", authFunctionBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthFunctionPO authFunctionPO = new AuthFunctionPO();
		copyAuthFunctionBO2AuthFunctionPO(authFunctionBO, authFunctionPO);
		Boolean updateResult = authFunctionDao.update(authFunctionPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新权限功能信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新权限功能信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAuthFunction(List<AuthFunctionBO> authFunctionBOList) throws ConsoleException
	{
		logger.info("====更新权限功能信息列表开始====");
		logger.debug("====更新权限功能信息列表.入参:List<AuthFunctionBO>=[{}]====", authFunctionBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthFunctionBO itemBO : authFunctionBOList)
		{
			ConsoleReturnBO itemResult = updateAuthFunction(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新权限功能信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新权限功能信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAuthFunction(AuthFunctionBO authFunctionBO)
	{
		logger.info("====删除权限功能信息开始====");
		logger.debug("====删除权限功能信息.入参:AuthFunctionBO＝[{}]======", authFunctionBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthFunctionPO authFunctionPO = new AuthFunctionPO();
		copyAuthFunctionBO2AuthFunctionPO(authFunctionBO, authFunctionPO);
		Boolean deleteResult = authFunctionDao.delete(authFunctionPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除权限功能信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除权限功能信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAuthFunction(List<AuthFunctionBO> authFunctionBOList) throws ConsoleException
	{
		logger.info("====删除权限功能信息列表开始====");
		logger.debug("====删除权限功能信息列表.入参:List<AuthFunctionBO>＝[{}]======", authFunctionBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthFunctionBO itemBO : authFunctionBOList)
		{
			ConsoleReturnBO itemResult = removeAuthFunction(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除权限功能信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除权限功能信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countAuthFunction(AuthFunctionBO authFunctionBO)
	{
		logger.info("====统计权限功能信息开始====");
		logger.debug("====统计权限功能信息。入参：AuthFunctionBO＝[{}]======", authFunctionBO);
		Long returnLong = new Long(0);
		AuthFunctionPO authFunctionPO = new AuthFunctionPO();
		copyAuthFunctionBO2AuthFunctionPO(authFunctionBO, authFunctionPO);
		returnLong = authFunctionDao.count(authFunctionPO);
		logger.debug("====统计权限功能信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计权限功能信息结束====");
		return returnLong;
	}

	private void copyAuthFunctionBO2AuthFunctionPO(AuthFunctionBO authFunctionBO, AuthFunctionPO authFunctionPO)
	{
		beanUtils.copyBeanNotNull(authFunctionBO, authFunctionPO);
		if (authFunctionBO.getSysID() != null)
		{
			authFunctionPO.setSeqID(authFunctionBO.getSysID());
		}
	}

	private void copyAuthFunctionPO2AuthFunctionBO(AuthFunctionPO authFunctionPO, AuthFunctionBO authFunctionBO)
	{
		beanUtils.copyBeanNotNull(authFunctionPO, authFunctionBO);
		if (authFunctionPO.getSeqID() != null)
		{
			authFunctionBO.setSysID(authFunctionPO.getSeqID());
		}
	}

}
